﻿using imaginecup.util;
using Microsoft.Xna.Framework;
using Imaginecup.util;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using Imaginecup.factory;
using Imaginecup.gameState.malaria.component.factory;
using System.Collections.Generic;
using Imaginecup.gameState.malaria.component.malaria;

namespace Imaginecup.gameState.malaria.manager
{
    public class MalariaGenerator
    {
        private int m_MaxMalariaNum;
        private ObjectFactory m_Factory;
        private ObjectManager m_ObjMan;

        public MalariaGenerator()
        {
            m_Factory = ObjectFactory.GetObjectFactory();
            m_CurrentTime = 0;
        }


        public void SetMalariaNum(int maxMalariaNum)
        {
            m_MaxMalariaNum = maxMalariaNum;
        }

        public void SetObjectManager(ObjectManager objMan)
        {
            m_Factory.SetObjectMan(objMan);
            m_ObjMan = objMan;
        }

        //public void GeneratorMalaria()
        //{
        //    EntityInfo malariaInfo = new EntityInfo();
        //    malariaInfo.mSize = new Vector2(35, 35);

        //    int count = m_ObjMan.GetNumofComponent(ComponentTypeID.CID_MALARIA_ENTITY);

        //    for (int i = 0; i < m_MaxMalariaNum - count; i++)
        //    {
        //        malariaInfo.mPosition = GetRandomPos( malariaInfo.mSize);

        //        ICmpEntity entity = (ICmpEntity)m_Factory.GetObject(MALARIA_COMPONENT_ID.MALARIA);
        //        entity.SetEntityInfo(malariaInfo);
        //    }
        //}


        
        private List<ChildInfo> m_ChildInfoList = new List<ChildInfo>();
        private double m_CurrentTime;


        public void AddChildInfo( ChildInfo info )
        {
            m_ChildInfoList.Add(info);
        }
      

        public void Update(GameTime gameTime)
        {
            m_CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;
            ChildInfo info;
            for( int i = m_ChildInfoList.Count - 1 ; i >= 0 ; i-- )
            {
                info = m_ChildInfoList[i];

                if( m_CurrentTime > info.m_AppearTime )
                {
                    GeneratorMalaria(info.m_Velocity);
                    m_ChildInfoList.RemoveAt(i);
                }
            }
        }

        private void GeneratorMalaria(double p)
        {
            ObjectFactory factory = ObjectFactory.GetObjectFactory();


            int CharSizeX = 50;
            int CharSizeY = 50;

            EntityInfo charInfo = new EntityInfo();

            charInfo.mSize = new Vector2(CharSizeX, CharSizeY);
            charInfo.mPosition = GetRandomPos(charInfo.mSize);


            CmpMalaria entity = (CmpMalaria)factory.GetObject(MALARIA_COMPONENT_ID.MALARIA);
            entity.SetEntityInfo(charInfo);
            entity.SetVelocity(p);
        }


        private Vector2 GetRandomPos( Vector2 charSize )
        {
            Vector2 ScreenSize = MalariaState.GetMalariaState().GetMainScreenSize();
            Vector2 pos = new Vector2();

            int CharSizeX = (int)charSize.X;
            int CharSizeY = (int)charSize.Y;
            int ScreenSizeX = (int)ScreenSize.X;
            int ScreenSizeY = (int)ScreenSize.Y;

            pos.X = RandomUtil.rand.Next(0, 800);
            pos.Y = RandomUtil.rand.Next(-CharSizeY, 0);

            //int rangeNum = RandomUtil.rand.Next(0 , 4);
            //switch( rangeNum )
            //{
            //    case 0:
            //        pos.X = RandomUtil.rand.Next(-CharSizeX - CharSizeX, -CharSizeX);
            //        pos.Y = RandomUtil.rand.Next(-CharSizeY, ScreenSizeY + CharSizeY);
            //        break;
            //    case 1:
            //        pos.X = RandomUtil.rand.Next(-CharSizeX, ScreenSizeX + CharSizeX);
            //        pos.Y = RandomUtil.rand.Next(-CharSizeY - CharSizeY, -CharSizeY);
            //        break;
            //    case 2:
            //        pos.X = RandomUtil.rand.Next(ScreenSizeX, ScreenSizeX + CharSizeX);
            //        pos.Y = RandomUtil.rand.Next(-CharSizeY, ScreenSizeY + CharSizeY);
            //        break;
            //    case 3:
            //        pos.X = RandomUtil.rand.Next(-CharSizeX, ScreenSizeX + CharSizeX);
            //        pos.Y = RandomUtil.rand.Next(ScreenSizeY, ScreenSizeY + CharSizeY);
            //        break;
            //}
            return pos;
        }
    }
}
